package com.parkinginfo.manager;

/*
 * Author: Deepika Gohil
 */


import java.util.ArrayList;

import com.parkinginfo.dbhelper.Place;
import com.parkinginfo.dbhelper.PlacesSQLiteOpenHelper;


import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class ParkingManagerApplication extends Application {
	
	public ArrayList<GarageObject> parkingGarages = null;
	
	private String Places_TABLE  = "places";
	private String Place_ID = "id";
	private  String Place_NAME = "name";

	private  String Place_LOCATION = "location";
	private  String Place_LAT = "lattitide";
	private  String Place_LON = "longitude";
	
	private SQLiteDatabase database;
	private ArrayList<Place> allplaces;
	
	@Override
	public void onCreate() {
		super.onCreate();
		PlacesSQLiteOpenHelper helper = new PlacesSQLiteOpenHelper(this);
		database = helper.getWritableDatabase();
		if (null == allplaces) {
			loadPlaces();
		}
	}

	@Override
	public void onTerminate() {
		database.close();
		super.onTerminate();
	}

	public void setPlaces(ArrayList<Place> currentTasks) {
		this.allplaces = currentTasks;
	}

	public ArrayList<Place> getPlaces() {
		return allplaces;
	}
	
	public void addPlace(Place t) {
		assert(null != t);
		
		ContentValues values = new ContentValues();
		values.put(Place_NAME, t.getName());
		values.put(Place_LOCATION, t.getLocation());
		values.put(Place_LAT, Double.toString(t.getLattitude()));
		values.put(Place_LON,  Double.toString(t.getLongitude()));
		
		t.setId(database.insert(Places_TABLE, null, values));

		allplaces.add(t);
	}
	
	public void savePlace(Place t) {
		assert(null != t);
		ContentValues values = new ContentValues();
		values.put(Place_NAME, t.getName());
		values.put(Place_LOCATION, t.getLocation());
		values.put(Place_LAT, Double.toString(t.getLattitude()));
		values.put(Place_LON,  Double.toString(t.getLongitude()));
		
		long id = t.getId();
		String where = String.format("%s = %d", Place_ID, id);

		database.update(Places_TABLE, values, where, null);
	}
	
	public void deletePlaces(Long[] ids) {
		StringBuffer idList = new StringBuffer();
		for (int i=0; i<ids.length; i++) {
			idList.append(ids[i]);
			if (i < ids.length - 1) {
				idList.append(",");
			}
		}
		String where = String.format("%s in (%s)", Place_ID, idList);
		database.delete(Places_TABLE, where, null);
	}

	public void loadPlaces() {
		allplaces = new ArrayList<Place>();
		Cursor tasksCursor = database.query(Places_TABLE, new String[] {Place_ID, Place_NAME, Place_LOCATION, Place_LAT, Place_LON}, null, null, null, null, String.format("%s", Place_NAME));
		tasksCursor.moveToFirst();
		Place t;
		if (! tasksCursor.isAfterLast()) {
			do {
				int id = tasksCursor.getInt(0);
				String name  = tasksCursor.getString(1);
				String location = tasksCursor.getString(2);
				
				String latString = tasksCursor.getString(3);
				String lonString = tasksCursor.getString(4);
				
				System.out.println(latString+","+lonString);
				double lat = Double.parseDouble(latString);
				double lon = Double.parseDouble(lonString);
				
				t = new Place(name);
				t.setId(id);
				t.setComplete(false);
				t.setLocation(location);
				t.setLattitude(lat);
				t.setLongitude(lon);
				allplaces.add(t);
			} while (tasksCursor.moveToNext());
		}
		tasksCursor.close();
	}
}
